﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using Rockfan.Framework;
using Rockfan.Framework.Graphics;

namespace Rockfan.Framework.Entities
{
    public sealed class Fretbutton : Sprite
    {
        public FretButtonType ButtonType { get; set; }

        public TextureInfo NormalTexture { get; set; }
        public TextureInfo PressedTexture { get; set; }
        public TextureInfo HitTexture { get; set; }

        public bool IsHit
        {
            get { return (ButtonType & FretButtonType.Hit) == FretButtonType.Hit; }
        }

        public bool IsPressed
        {
            get { return (ButtonType & FretButtonType.Pressed) == FretButtonType.Pressed; }
        }

        public Fretbutton(FretButtonType type)
            :base(null)
        {
            ButtonType = type;

            NormalTexture = new TextureInfo();
            PressedTexture = new TextureInfo();
            HitTexture = new TextureInfo();
        }

        public void Release()
        {
            ButtonType &= ~FretButtonType.Hit;
            ButtonType &= ~FretButtonType.Pressed;
            ButtonType |= FretButtonType.Released;
        }

        /// <summary>
        /// Swaps the position of the note over an amout of time in seconds
        /// </summary>
        /// <param name="delta">a float value indicating the amount of time in seconds it should take to swap the position of this note</param>
        public void Swap(float duration)
        {
            if (this.X == 0 || SwapDurationRemaining > 0)
                return;

            SwapDuration = duration;
            SwapDurationRemaining = duration;
            StartPosition = this.X;
            EndingPosition = this.X * -1;
        }

        public void UpdateState(float delta)
        {
            this.Update(delta);
        }

        protected override void Update(float delta)
        {
            if (IsHit)
            {
                if (HitTexture != null)
                {
                    this.TopTextureCoordinate = HitTexture.TopTextureCoordinate;
                    this.BottomTextureCoordinate = HitTexture.BottomTextureCoordinate;
                    this.LeftTextureCoordinate = HitTexture.LeftTextureCoordinate;
                    this.RightTextureCoordinate = HitTexture.RightTextureCoordinate;
                    this.Texture = HitTexture.Texture;
                }
            }
            else if (IsPressed)
            {
                if (PressedTexture != null)
                {
                    this.TopTextureCoordinate = PressedTexture.TopTextureCoordinate;
                    this.BottomTextureCoordinate = PressedTexture.BottomTextureCoordinate;
                    this.LeftTextureCoordinate = PressedTexture.LeftTextureCoordinate;
                    this.RightTextureCoordinate = PressedTexture.RightTextureCoordinate;
                    this.Texture = PressedTexture.Texture;
                }
            }
            else if ((ButtonType & FretButtonType.Released) == FretButtonType.Released)
            {
                if (NormalTexture != null)
                {

                    this.TopTextureCoordinate = NormalTexture.TopTextureCoordinate;
                    this.BottomTextureCoordinate = NormalTexture.BottomTextureCoordinate;
                    this.LeftTextureCoordinate = NormalTexture.LeftTextureCoordinate;
                    this.RightTextureCoordinate = NormalTexture.RightTextureCoordinate;
                    this.Texture = NormalTexture.Texture;
                }

                ButtonType &= ~FretButtonType.Released;
            }
            else
            {
                if (NormalTexture != null)
                {

                    this.TopTextureCoordinate = NormalTexture.TopTextureCoordinate;
                    this.BottomTextureCoordinate = NormalTexture.BottomTextureCoordinate;
                    this.LeftTextureCoordinate = NormalTexture.LeftTextureCoordinate;
                    this.RightTextureCoordinate = NormalTexture.RightTextureCoordinate;
                    this.Texture = NormalTexture.Texture;
                }
            }

            if (SwapDurationRemaining > 0.0f)
            {
                this.X -= ((StartPosition - EndingPosition) / SwapDuration) * delta;

                SwapDurationRemaining -= delta;

                if (SwapDurationRemaining <= 0)
                    this.X = EndingPosition;
            }

            base.Update(delta);
        }

        private float SwapDuration;
        private float StartPosition;
        private float EndingPosition;
        private float SwapDurationRemaining;
    }
}
